package com.alon.spring01.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alon.spring01.common.config.redis.RedisUtil;
import com.alon.spring01.common.model.Result;
import com.alon.spring01.mapper.CountryMapper;
import com.alon.spring01.model.Country;
import com.alon.spring01.service.CountryService;
import com.alon.spring01.utils.ResultUtil;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @program: spring01
 * @description: 控制器
 * @author: yulongguang
 * @create: 2019-04-06 16:21
 **/
@RestController
@Slf4j
@RequestMapping("/country")
@Api("CountryController相关的api")
public class CountryController {
    ExecutorService service = Executors.newFixedThreadPool(100);
    @Autowired
    private CountryMapper countryMapper;
    @Autowired
    private CountryService countryService;

    @ApiOperation(value = "新增城市数据", notes = "新增城市数据")
    @PostMapping("/insert")
    public Result<?> insert() {
        for (int i = 0; i < 10; i++) {

            Country country = new Country();
            country.setCountryname("中国");
            country.setCountrycode("021"+i);
            RedisUtil.set(country.getCountrycode(), JSON.toJSONString(country));
             countryMapper.insert(country);
            log.info("hhhhhhhhhhhhhhhhh-------------------种种22112");
        }
        return ResultUtil.ok();
    }
    @ApiOperation(value = "查询所有", notes = "查询所有")
    @GetMapping("/selectAll")
    public Result<?> selectAll() {

        String result= (String) RedisUtil.get("0213");
        JSONObject jsonObject = JSONObject.parseObject(result);
        boolean notNull = ObjectUtil.isNotNull(jsonObject);

        for (int i=0;i<100;i++){
            service.execute(()->{
                boolean lock = RedisUtil.lock("021");
                System.out.println(lock);
            });

        }
        if (notNull){
//            Convert.convert();
//            DateUtil.between(,, DateUnit.HOUR);
            Country country= JSON.parseObject(jsonObject.toJSONString(), Country.class);
            log.info("redis返回的数据 || country:{}",JSON.toJSONString(country));
            return ResultUtil.ok(countryMapper.selectAll());
        }
        return ResultUtil.ok();
    }
    @ApiOperation(value = "根据参数查询数据", notes = "根据参数查询数据")
    @ApiImplicitParam(name = "countryname", value = "国家名称", paramType = "path", dataType = "String")
    @GetMapping("/selectByParams/{countryname}")
    public Result<?> selectByParams(@PathVariable String countryname) {
        return ResultUtil.ok(countryMapper.selectCountryname(countryname));
    }
    @ApiOperation(value = "根据参数查询数据", notes = "根据参数查询数据")
    @GetMapping("/selectByPage")
    public Result<?> selectByPage(@ApiParam(name = "page", value = "分数") int page,
                                  @ApiParam(name = "pageSize", value = "分页大小") int pageSize) {
        return ResultUtil.ok(countryService.findwithPage(page, pageSize));

    }
}
